package b.h.y.bj;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import b.h.y.bj.fragment.ContentFragment;
import b.h.y.bj.fragment.LeftMenuFragment;

public class MainActivity extends SlidingFragmentActivity {

    private static final String FRAGMENT_LEFT_MENU = "fragment_left_menu";
    private static final String FRAGMENT_CONTENT = "fragment_content";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setBehindContentView(R.layout.left_menu); //设置侧边栏

        SlidingMenu slidingMenu = getSlidingMenu(); //获取侧边栏
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); //设置全屏触摸
        slidingMenu.setBehindOffset(360); //设置预留屏幕宽度

        initFragment();
    }

    /**
     * 初始化fragment,将fragment数据填充给布局文件
     */
    private void initFragment() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();//开启事务

        // argument[2]: 打标记,类似与唯一ID一样, 使用 supportFragmentManager.findFragmentByTag(FRAGMENT_LEFT_MENU);
        fragmentTransaction.replace(R.id.fl_layout_left_menu, new LeftMenuFragment(), FRAGMENT_LEFT_MENU); //用fragment替换fragmentlayout
        fragmentTransaction.replace(R.id.fl_layout_content, new ContentFragment(), FRAGMENT_CONTENT); //用fragment替换fragmentlayout
        fragmentTransaction.commit();
    }

    /**
     * 获取侧边栏
     *
     * @return
     */
    public LeftMenuFragment getLeftMenuFragment() {
        FragmentManager manager = getSupportFragmentManager();
        LeftMenuFragment leftMenuFragment = (LeftMenuFragment) manager.findFragmentByTag(FRAGMENT_LEFT_MENU);
        return leftMenuFragment;
    }

    /**
     * 获取侧边栏
     *
     * @return
     */
    public ContentFragment getContentFragment() {
        FragmentManager manager = getSupportFragmentManager();
        ContentFragment contentFragment = (ContentFragment) manager.findFragmentByTag(FRAGMENT_CONTENT);
        return contentFragment;
    }
}
